<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // [1,2,3,5,7,8,10]
      // [1~3,5,7~8,10]
      let result = [];
      let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
      let left = 0;
      let right = 0;
      while (right < arr.length) {
        if (arr[right + 1] - arr[right] === 1) {
          right++;
        } else {
          if (left === right) {
            result.push(arr[left]);
            left++;
          } else {
            result.push(`${arr[left]}~${arr[right]}`);
            left = right + 1;
          }
          right++;
        }
      }
      console.log(result);
    </script>
  </body>
</html>
